package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.dao.OrderItemRepostory;
import com.dongdongshop.dao.OrderRepostory;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.feignservice.AddressFeignService;
import com.dongdongshop.feignservice.CartFeignService;
import com.dongdongshop.pojo.cart.Cart;
import com.dongdongshop.pojo.order.TbOrder;
import com.dongdongshop.pojo.order.TbOrderItem;
import com.dongdongshop.pojo.user.TbAddress;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.order.OrderService;
import com.dongdongshop.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/4/4 23:19
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepostory orderRepostory;

    @Autowired
    private OrderItemRepostory orderItemRepostory;

    @Resource
    private AddressFeignService addressFeignService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private CartFeignService cartFeignService;


    @Override
    public void addOrder(List<Cart> cartList, Long idl, TbUser tbUser) {
        //查询该收件人地址
        DataResult<TbAddress> dataResult = addressFeignService.findAddressById(idl);
        TbAddress address = dataResult.getData();
        //从该用户的redis购物车中删除该商品明细  用于删除的
        List<Cart> carts = (List<Cart>)redisTemplate.boundHashOps("cartList").get(tbUser.getUsername());

        //大订单
        TbOrder order = new TbOrder();
        for (Cart cart : cartList) {
            Long orderId = idWorker.nextId();//雪花算法生成订单Id
            order.setOrderId(orderId);//订单号
            order.setStatus("1");//订单状态 未付款
            order.setCreateTime(new Date());//订单创建时间
            order.setUserId(tbUser.getId().toString());//买家id
            order.setBuyerMessage(tbUser.getName());//买家昵称
            order.setReceiverAreaName(address.getAddress());//收货地址
            order.setReceiverMobile(address.getMobile());//收货人手机
            order.setReceiver(address.getContact());//收货人姓名
            order.setSellerId(cart.getSellerId());//商家id
            order.setPaymentType("1");//付款方式    1、在线支付，2、货到付款'

            //商品明细
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem orderItem : orderItemList) {
                TbOrderItem ot = new TbOrderItem();
                ot.setId(idWorker.nextId());//主键id
                ot.setItemId(orderItem.getItemId());//该商品明细订单关联的商品明细的主键id
                ot.setGoodsId(orderItem.getGoodsId());//spuId
                ot.setOrderId(orderId);//订单id
                ot.setTitle(orderItem.getTitle());//标题
                ot.setPrice(orderItem.getPrice());//单价
                ot.setNum(orderItem.getNum());//数量
                ot.setTotalFee(orderItem.getTotalFee());//总价钱
                ot.setPicPath(orderItem.getPicPath());//图片
                ot.setSellerId(cart.getSellerId());//商家id

                //计算大订单的总价
                BigDecimal payment = order.getPayment();
                Integer paymentInt;
                if(payment != null){
                    paymentInt = payment.intValue();
                }
                paymentInt = 0;
                BigDecimal totalFee = orderItem.getTotalFee();
                Integer totalFeeInt = totalFee.intValue();
                Integer totalPriceInt = paymentInt + totalFeeInt;
                BigDecimal bigDecimal = new BigDecimal(totalPriceInt);
                order.setPayment(bigDecimal);
                //结束

                //添加小订单
                orderItemRepostory.save(ot);
                //从该用户的redis购物车中删除该商品明细
                DataResult<List<Cart>> dataResult1 = cartFeignService.deleteCartOrderItem(carts, orderItem.getItemId());
                carts = dataResult1.getData();
            }
        }
        //添加大订单
        orderRepostory.save(order);
        //最后将删除后的购物车重新添加到该用户的redis中
        String cartsStr = JSONObject.toJSONString(carts);
        cartFeignService.save(cartsStr, tbUser.getUsername());
    }
}
